<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-05-18
 * Time: 16:59
 */

namespace plugins\wechat;

use cmf\lib\Plugin;
use think\Db;

class WechatPlugin extends Plugin{
    public $info = [
        'name' => 'Wechat',
        'title'=> '微信公众号插件',
        'description' =>'微信公众号插件',
        'status'    => 1,
        'author'    => '一土小哥哥',
        'version'   =>  '1.0'
    ];

    public $hasAdmin = 1;       //插件是否有后台

    public function install()
    {
        // TODO: Implement install() method.
        $db_config = config('database');    //读取数据库配置
        $db_sql = cmf_split_sql(PLUGINS_PATH.'wechat/data/cmf_plugin_wechat.sql',$db_config['prefix'],$db_config['charset']);       //读取sql文件
        if (empty($db_config) || empty($db_sql)){
            $this->error('非法安装');
        }
        $db = Db::connect($db_config);
        foreach ($db_sql as $k=>$sql){
            try{
                $db->execute($sql);
            }catch(\Exception $e){
                return false;
            }
        }
        return true;
    }
    public function uninstall()
    {
        // TODO: Implement uninstall() method.
        $db_config = config('database');
        $db_sql = [
            "DROP TABLE IF EXISTS ".$db_config['prefix']."plugin_wechat_config",
            "DROP TABLE IF EXISTS ".$db_config['prefix']."plugin_wechat_menu",
            "DROP TABLE IF EXISTS ".$db_config['prefix']."plugin_wechat_reply",
        ];
        if (empty($db_config)){
            $this->error('非法卸载');
        }
        $db = Db::connect($db_config);
        foreach ($db_sql as $k=>$sql){
            try{
                $db->execute($sql);
            }catch(\Exception $e){
                return false;
            }

        }
        return true;
        //dump($db_sql);
    }
}